﻿/*-----------------------------------------------------------------------------------------
   Author               Date            Description
   ----------------------------------------------------------------------------------------
   Steven Applegate     01/30/2011      Initial creation
 ----------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace SuddenDeath.Domain.Services.Email
{
    public class Emailer
    {
        /// <summary>
        /// Author: Steven Applegate (05/09/2011)
        /// Description: The default e-mail address where e-mails come from
        /// </summary>
        public static string DefaultFrom
        {
            get { return ConfigurationManager.AppSettings["EmailDefaultFrom"];  }
        }



        /// <summary>
        /// Author: Steven Applegate (05/09/2011)
        /// Description: Sends an e-mail
        /// </summary>
        private static void SendEmail(IEnumerable<MailAddress> to, IEnumerable<MailAddress> bcc, MailAddress from, string subject, string bodyHtml)
        {
            var mail = new MailMessage { From = from, Subject = subject, Body = bodyHtml, IsBodyHtml = true };

            foreach (var address in to)
            {
                mail.To.Add(address);
            }

            foreach (var address in bcc)
            {
                mail.Bcc.Add(address);
            }

            try
            {
                string server = ConfigurationManager.AppSettings["SMTPServer"];
                int port = Int32.Parse(ConfigurationManager.AppSettings["SMTPPort"]);
                string username = ConfigurationManager.AppSettings["SMTPUsername"];
                string password = ConfigurationManager.AppSettings["SMTPPassword"];

                var smtp = new SmtpClient
                               {
                                   Host = server,
                                   Port = port,
                                   EnableSsl = true,
                                   DeliveryMethod = SmtpDeliveryMethod.Network,
                                   UseDefaultCredentials = false,
                                   Credentials = new NetworkCredential(username, password)
                               };

                smtp.Send(mail);
            }
            catch (Exception err)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(err);
            }
        }



        /// <summary>
        /// Author: Steven Applegate (05/09/2011)
        /// Description: Sends an e-mail
        /// </summary>
        public static void SendEmail(string[] to, string[] bcc, string subject, string bodyHtml)
        {
            var addresses = new MailAddressCollection();
            var bccAddresses = new MailAddressCollection();

            foreach (var address in to)
            {
                addresses.Add(address);
            }

            foreach (var address in bcc)
            {
                bccAddresses.Add(address);
            }

            SendEmail(addresses, bccAddresses, new MailAddress(DefaultFrom), subject, bodyHtml);
        }
    }
}
